﻿#include "TheTimingWidget.h"
#include <QFile>

#define MAXTIME 86400

TheTimingWidget::TheTimingWidget(QWidget *parent)
    : QWidget(parent)
    , m_iSumTime(0)
    , m_iTime(0)
    , m_iMinute(0)
    , m_iSecond(0)
    , m_pTimer(nullptr)
{
    InitUi();

    InitProperty();
}

TheTimingWidget::~TheTimingWidget()
{
    if(m_pTimer->isActive())
    {
        m_pTimer->stop();
        m_pTimer->deleteLater();
    }
}

int TheTimingWidget::GetSumTime() const
{
    return m_iSumTime;
}

void TheTimingWidget::InitUi()
{
    m_pRootLayout = new QHBoxLayout(this);
    m_pRootLayout->setContentsMargins(0,0,0,0);
    m_pRootLayout->setSpacing(0);

    m_pTimeLabel = new QLabel(this);
    m_pTimeLabel->setObjectName("TimeLabel");
    m_pTimeLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pTimeLabel->setText("00");
    m_pColon_0 = new QLabel(this);
    m_pColon_0->setObjectName("Colon");
    m_pColon_0->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pColon_0->setText(":");
    m_pMinuteLabel = new QLabel(this);
    m_pMinuteLabel->setObjectName("MinuteLabel");
    m_pMinuteLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pMinuteLabel->setText("00");
    m_pColon_1 = new QLabel(this);
    m_pColon_1->setObjectName("Colon");
    m_pColon_1->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pColon_1->setText(":");
    m_pSecondLabel = new QLabel(this);
    m_pSecondLabel->setObjectName("SecondLabel");
    m_pSecondLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pSecondLabel->setText("00");

    m_pRootLayout->addWidget(m_pTimeLabel);
    m_pRootLayout->addWidget(m_pColon_0);
    m_pRootLayout->addWidget(m_pMinuteLabel);
    m_pRootLayout->addWidget(m_pColon_1);
    m_pRootLayout->addWidget(m_pSecondLabel);

}

void TheTimingWidget::InitProperty()
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Background,Qt::transparent);
    setPalette(pal);
    setWindowOpacity(1);
    setAttribute(Qt::WA_TranslucentBackground);

    //this->setMinimumSize(QSize(500, 300));

    QFile resourceqss(":/qss/TheTimingWidget.qss");
    resourceqss.open(QFile::ReadOnly);
    this->setStyleSheet(resourceqss.readAll());
    resourceqss.close();

    m_pTimer = new QTimer(this);

    connect(m_pTimer, &QTimer::timeout, this, [=](){
        if(m_iSumTime >= MAXTIME)
        {
            m_pTimer->stop();
            emit timeout();
            return;
        }
        m_iSumTime++;
        emit updatetime(m_iSumTime);
        UpdateTimer(m_iSumTime);
        if(m_iSumTime >= MAXTIME)
        {
            m_pTimer->stop();
            emit timeout();
            return;
        }
    });
}

void TheTimingWidget::UpdateTimer(int timer)
{
   m_iTime = timer/3600;
   m_iMinute = timer%3600/60;
   m_iSecond = timer%3600%60;

   m_pTimeLabel->setText(QString::number(m_iTime/10) + QString::number(m_iTime%10));
   m_pMinuteLabel->setText(QString::number(m_iMinute/10) + QString::number(m_iMinute%10));
   m_pSecondLabel->setText(QString::number(m_iSecond/10) + QString::number(m_iSecond%10));

}

void TheTimingWidget::on_start(bool start)
{
    if(start)
    {
        m_pTimer->start(1000);
    } else {
        m_pTimer->stop();
    }
}

void TheTimingWidget::on_reset()
{
    m_iSumTime = 0;
    m_iTime = m_iSumTime/3600;
    m_iMinute = m_iSumTime%3600/60;
    m_iSecond = m_iSumTime%3600%60;

    m_pTimeLabel->setText(QString::number(m_iTime/10) + QString::number(m_iTime%10));
    m_pMinuteLabel->setText(QString::number(m_iMinute/10) + QString::number(m_iMinute%10));
    m_pSecondLabel->setText(QString::number(m_iSecond/10) + QString::number(m_iSecond%10));
}
